#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket,os,re 

'''
пытаемся общацца с AniDB
http://wiki.anidb.net/w/UDP_API_Definition#Server_.2F_UDP_Connection

http://dev.jvnv.net/misc/file/trunk/pyanidb/pyanidb/__init__.py

http://www.python.org/doc/current/lib/module-socket.html
'''

HOST='api.anidb.info'
PORT=9000
login='apkawa'
password='xerosexo'

file='Scrapped Princess - 01.mkv'
class anidb:
    def __init__(self):
        self.sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.bind(('0.0.0.0', 1234))
        self.sock.connect((HOST,PORT))
    def __del__(self):
        sock.send('LOGOUT s=%s'%session[1])
        print sock.recv(1024)
        sock.close()
    def auth(self,login,password):
        self.sock.send('AUTH user=%s&pass=%s&protover=3&client=pyanidb&clientver=0'%(login,password))
        self.session=sock.recv(1024).split()[1]
        return self.session
    def findfile(self,file):
        self.ed2k=ed2k(file)
        self.sock.send('FILE size=%s&ed2k=%s&s=%s'%(ed2k[0],ed2k[1],session))
        return

def ed2k(file):
    a=os.popen('ed2k_hash "%s"' %file).read().split('|')
    size,ed2k=a[3],a[4]
    return size,ed2k

def parsed_reliz(namefile):
    group=re.findall('\[.+?\]',namefile)[0]
    return group


'''

sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 1234))
sock.connect((HOST,PORT))
sock.send('AUTH user=%s&pass=%s&protover=3&client=pyanidb&clientver=0'%(login,password))
session=sock.recv(1024).split()[1] #Размер пакета.
#print session


print parsed_reliz(sock.recv(1024).split('|')[-1])

sock.send('LOGOUT s=%s'%session[1])
print sock.recv(1024)
sock.close()
'''
